package com.sd.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import android.os.Bundle;

import com.alex.http.AHandleable;
import com.alex.http.AHandledResult;
import com.alex.http.AHttpRequest;
import com.alex.log.ALog;
import com.sd.app.AppInit;

public class DownloaderApk implements AHandleable {

	static final String ITEM = "item";
	private int currentstate = -1;
	final int TITLE = 1;
	final int LINK = 2;
	final int DESCRIPTION = 3;
	final int PUBDATE = 4;

	
	@Override
	public AHandledResult handle(long requestId, String paramString,
			Bundle paramBundle) {
		// TODO Auto-generated method stub
		ALog.e("handle");
		return null;
	}

	@Override
	public AHandledResult handle(long requestId, InputStream paramInputStream,
			Bundle paramBundle) {
		// TODO Auto-generated method stub
		File file = new File(AppInit.getInstance().APP_DIR, "booknow.apk");
		if (file.exists()) {
			file.delete();
		}
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(file);
			byte[] buf = new byte[2048];
			int num = -1;
			boolean flag = false;
			while ((num = paramInputStream.read(buf)) != -1) {
				fos.write(buf, 0, num);
				fos.flush();
				flag = true;
			}
			fos.close();
			byte[] body = null;
			return new AHandledResult(null, null, AppInit.getInstance().APP_DIR+"booknow.apk");
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

	@Override
	public int getContentType() {
		// TODO Auto-generated method stub
		return AHttpRequest.TYPE_STREAM;
	}

}